package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 班级列表查询（分页查询）
     *
     * @param clazzQueryParam
     * @return
     */
    @Override
    public PageResult<Clazz> pageQuery(ClazzQueryParam clazzQueryParam) {
        // 设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());

        Page<Clazz> page = clazzMapper.pageQuery(clazzQueryParam);

        return new PageResult<>(page.getTotal(), page.getResult());
    }


    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }


    /**
     * 添加班级
     *
     * @param clazz
     */
    @Override
    public void add(Clazz clazz) {
        // 补全基础属性
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(clazz.getCreateTime());
        clazzMapper.insert(clazz);
    }


    /**
     * 根据班级id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getByClazzId(Integer id) {
        Clazz clazz = clazzMapper.getByClazzId(id);
        return clazz;
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }


    /**
     * 查询所有班级
     *
     * @return
     */
    @Override
    public List<Clazz> list() {
        return clazzMapper.selectAll();
    }


}
